package com.gcb.controller;

import com.gcb.constant.MessageConstant;
import com.gcb.entity.Result;
import com.gcb.pojo.OrderSetting;
import com.gcb.service.OrderSettingService;
import com.gcb.util.POIUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author 35235
 * @Date 2021/8/4 20:07
 * @Version 1.0
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @DubboReference
    OrderSettingService orderSettingService;

    @RequestMapping("/getOrderSettingByMonth.do")
    public Result getOrderSettingByMonth(@DateTimeFormat(pattern = "yyyy-MM") Date date) {
        System.out.println(date);
        System.out.println(date.getTime());
        List<Map> orderSettings = orderSettingService.getOrderSettingByMonth(date);
        if (null!=orderSettings){
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,orderSettings);
        }else {
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }

    }

    @RequestMapping("/editNumberByDate.do")
    public Result editNumberByDate(@RequestBody Map data) {
        Integer number = Integer.parseInt(data.get("number") + "");
        boolean updateResult = false;
        boolean insertResult = false;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            OrderSetting os = new OrderSetting();
            Date date = format.parse(String.valueOf(data.get("orderDate")));
            os.setOrderDate(date);
            os.setNumber(number);
            updateResult = orderSettingService.updateNumber(os);
            if (false == updateResult) {
                insertResult = orderSettingService.add(os);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        System.out.println("data = " + data);
//        Date = data.get("orderDate")
//        System.out.println(number + "," +orderDate);
        if (true == insertResult || true == updateResult) {
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } else {
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }
    @RequestMapping("/upload.do")
    public Result upload(@RequestParam("excelFile")MultipartFile excelFile){
        //使用POI解析表格数据
        List<String[]> list = null;
        try {
            list = POIUtils.readExcel(excelFile);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        for (String[] strings : list){
            String orderDate = strings[0];
            String number = strings[1];
            OrderSetting orderSetting = new OrderSetting(new Date(orderDate),Integer.parseInt(number));
            boolean updateNumber = orderSettingService.updateNumber(orderSetting);
            if (false == updateNumber){
                orderSettingService.add(orderSetting);
            }
        }
        //通过dubbo远程调用服务实现数据批量导入到数据库
        return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }
}
